package com.bwie.bwedu.fragment;

import android.content.Intent;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.activity.StudentInfoActivity;
import com.bwie.bwedu.adapter.LocalSaveAdapter;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.bean.response.BaseResponse;
import com.bwie.bwedu.bean.response.MainInfoResponse;
import com.bwie.bwedu.presenter.impl.LocalSavePresenterImpl;
import com.bwie.bwedu.ui.dialog.CustomBaseDialog;
import com.bwie.bwedu.utils.LogUtils;
import com.bwie.bwedu.view.LocalSaveView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/21.
 * 本地保存界面
 */
public class LocalSaveFragment extends BaseFragment implements LocalSaveView, LocalSaveAdapter.ItemClickListener {

    public static final int REQUEST_CODE = 200;
    public static final int RESULT_UPLOAD_CODE = 300;
    public static final int RESULT_LOACL_CODE = 400;

    private View view;
    private PullToRefreshListView pullToRefreshListView;
    private int pn = 1;
    private LocalSavePresenterImpl localSavePresenter;
    private LocalSaveAdapter adapter;
    List<Student> list;
    private CustomBaseDialog dialog;
    private int pno;

    @Override
    public View getView(LayoutInflater inflater) {
        list = new ArrayList<Student>();
        view = inflater.inflate(R.layout.fragment_localsave, null);
        return view;
    }

    @Override
    public void findView() {
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.track_list);
    }

    @Override
    public void setData() {
        //网络请求
        localSavePresenter = new LocalSavePresenterImpl(this);
        localSavePresenter.presenter(2 + "", pn + "");
    }

    @Override
    public void setListener() {
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pn = 1;
                localSavePresenter.presenter(2 + "", pn + "");
            }

            //上拉加载
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pn++;
                localSavePresenter.presenter(2 + "", pn + "");
            }
        });
        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //TODO 直接跳转指编辑

            }
        });
    }

    //请求成功
    @Override
    public void onSuccess(MainInfoResponse userRespons) {
        if (adapter == null) {
            if (userRespons.list!=null){
                list.addAll(userRespons.list);
            }
            adapter = new LocalSaveAdapter(getActivity(), list);
            adapter.setItemClickListener(this);
            pullToRefreshListView.setAdapter(adapter);
        } else if (pn == 1 && userRespons.list != null && userRespons.list.size() != 0) {
            LogUtils.d(list.size() + "------------------------WaittingConfirmFragment----------------------------------");
            list.clear();
            if (userRespons.list!=null){
                list.addAll(userRespons.list);
            }
            adapter.notifyDataSetChanged();
        }
        if (pn != 1 && userRespons.list != null && userRespons.list.size() != 0) {
            if (userRespons.list!=null){
                list.addAll(userRespons.list);
            }
            adapter.notifyDataSetChanged();
        }
        pullToRefreshListView.onRefreshComplete();
    }

    //请求失败
    @Override
    public void onFail(String info) {
        showToast(info);
        adapter.setItemClickListener(this);
    }

    //上传请求成功的返回
    @Override
    public void InvalidSuccess(MainInfoResponse userResponse) {
//        dialog.setListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
////                showToast(userResponse.msg);
//            }
//        });
//        dialog.cancel();
    }

    //上传请求失败的返回
    @Override
    public void InvalidError(String info) {
        showToast(info);
    }

    //删除成功方法
    @Override
    public void deleteSuccess(BaseResponse baseResponse) {
        list.remove(list.get(pno));
        adapter.notifyDataSetChanged();
    }

    //删除失败方法
    @Override
    public void deleteError(String info) {
        showToast(info+"");
    }

    @Override
    public void onUpLoadFail(final String info) {
        Looper.prepare();
//        if (Looper.myLooper() != Looper.getMainLooper()){
//            getActivity().runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    dialog = new CustomBaseDialog(getActivity());
//                    dialog.show();
//                    dialog.setText(""+info);
//                }
//            });
//            return;
//        }
        dialog = new CustomBaseDialog(getActivity());
        dialog.show();
        dialog.setText("" + info);
//        if (Looper.myLooper() != Looper.getMainLooper()){
//        }
        Looper.loop();
    }

    @Override
    public void onUpLoadSuccess() {
        list.get(pno).flag="2";
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    //上传操作
    @Override
    public void onInvalidClick(final int position) {
        //无效反馈方法
//        1.弹出对话框
        dialog = new CustomBaseDialog(getActivity());
        dialog.show();
        dialog.setText("是否提交该信息");
        dialog.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pno = position;
                LogUtils.e("===========fragment============");
                dialog.dismiss();
                localSavePresenter.upLoad(list.get(position));
            }
        });
//        presenter = new WaitingConfrimFragmentPresenterImpl(this);
//        presenter.presenter(id);
    }

    //    onValidClick
//编辑操作
    @Override
    public void onValidClick(int position) {
        pno = position;
        Intent intent = new Intent(getActivity(), StudentInfoActivity.class);
        intent.putExtra("id",list.get(position).id);
//        intent.putExtra("student", list.get(position));
        startActivityForResult(intent, REQUEST_CODE);
//                startActivity(intent);
    }

    //删除接口
    @Override
    public void deleteClick(int position) {
        this.pno = position;
        dialog = new CustomBaseDialog(getActivity());
        dialog.show();
        dialog.setText("是否将该条学员信息删除？");
        dialog.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                localSavePresenter.deletePresenter(list.get(pno).id);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_UPLOAD_CODE) {
            Student stu = (Student) data.getSerializableExtra("stu");
            stu.flag="2";
            list.set(pno,stu);
            adapter.notifyDataSetChanged();
        } else if (requestCode == REQUEST_CODE && resultCode == RESULT_LOACL_CODE) {
            Student stu = (Student) data.getSerializableExtra("stu");
            list.set(pno,stu);
            adapter.notifyDataSetChanged();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    public void addList(Student student){
        if (list!=null&&adapter!=null){
            list.add(0,student);
            adapter.notifyDataSetChanged();
        }
    }
}
